package com.sh.集合;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class TreeMap和LinkedHashMap {
    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();

        map.put("banana", 3);
        map.put("apple", 5);
        map.put("cherry", 2);
        map.put("date", 10);

        // 按 key 自动排序（字典序）
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());
        }

        // 获取第一个和最后一个 key
        System.out.println("First key: " + map.firstKey());
        System.out.println("Last key: " + map.lastKey());

        // 获取小于某个 key 的 entry
        System.out.println("Lower than 'cherry': " + map.lowerKey("cherry"));

        LinkedHashMap<String, Integer> objectObjectLinkedHashMap = new LinkedHashMap<>();
        objectObjectLinkedHashMap.put("banana", 3);
        objectObjectLinkedHashMap.put("apple", 5);
        objectObjectLinkedHashMap.put("cherry", 2);
        objectObjectLinkedHashMap.put("date", 10);
        for (Map.Entry<String, Integer> entry : objectObjectLinkedHashMap.entrySet()) {
            System.out.println(entry.getKey() + " => " + entry.getValue());
        }

        HashMap<String, Integer> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("banana", 3);
        objectObjectHashMap.put("apple", 5);
        objectObjectHashMap.put("cherry", 2);
        objectObjectHashMap.put("date", 10);
        objectObjectHashMap.forEach((key, value) -> System.out.println(key + " => " + value));

        ThreadLocal<Object> objectThreadLocal = new ThreadLocal<>();
        objectThreadLocal.set("123");
        System.out.println(objectThreadLocal.get());
    }
}

